package net.starwifi.duobao.service.impl;

import net.starwifi.duobao.entity.*;
import net.starwifi.duobao.repository.GglUserRepository;
import net.starwifi.duobao.repository.GglhdRepository;
import net.starwifi.duobao.service.*;
import net.starwifi.duobao.utils.times.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GglUserServiceImpl extends BaseServiceImpl<GglUser,Long> implements GglUserService {


    //@Autowired
    //MemberRepository repository;
    @Autowired
    GglUserRepository gglUserRepository;

    @Autowired
    GglhdRepository gglhdRepository;

    @Autowired
    SettingService settingService;

@Autowired
MemberService memberService;
    @Override
    public List<GglUser> ListUser(Long memberId ,int status ,int hdType){
        return gglUserRepository.ListUser(memberId , status,hdType);
    }
    @Override
    public List<GglUser> ListUsere(Long memberId ,int type,Date gainDate){
        return gglUserRepository.ListUsere(memberId, type, gainDate);
    }

    @Override
    public  GglUser FindMemberid(Long memberId,Date gainDate,int type){
        return gglUserRepository.FindMemberid(memberId,gainDate,type);
    }

//    @Override
//     public void findAuthMemberId(int status,Date gainDate,int oldstatus,int type,int hdType){
//         gglUserRepository.findAuthMemberId(status, gainDate,  oldstatus,type, hdType);
//    }

    /**
     * 登录奖励-刮刮乐
     * @param member
     */
    @Override
    public void saveLog(net.starwifi.duobao.entity.Member member){
        Date gainDate= DateTool.setHms(new Date(), 0, 0, 0);
        int type =0;
        GglUser  gglUser =  FindMemberid( member.getId(), gainDate,type);
        int status =1;

        List<Gglhd> gglhd = gglhdRepository.findByStatus(status,type);
        if(gglhd !=null){
            if (gglUser==null){
                gglUser = new GglUser();
                gglUser.setStatus(0);
                gglUser.setType(0);
                gglUser.setHdType(0);
                gglUser.setGainDate(gainDate);
                gglUser.setMemberId(member.getId());
                gglUser.setMobile(member.getMobile());
                save(gglUser);
            }
        }


    }

    /**
     * 充值奖励-刮刮乐
     * @param member
     */
    @Override
    public void consume(net.starwifi.duobao.entity.Member member){
        Date gainDate= DateTool.setHms(new Date(), 0, 0, 0);
        Setting setting = settingService.findByCode("GglMax");
        int GglMax = Integer.parseInt(setting.getConfigValue());
         setting = settingService.findByCode("GglMin");
        int GglMin = Integer.parseInt(setting.getConfigValue());
        int type =1;
        List<GglUser> gglUsers= gglUserRepository.ListUsere( member.getId(),type, gainDate);
        int status =1;
        List<Gglhd> gglhd = gglhdRepository.findByStatus(status,type);
        if(gglhd !=null){
            if (gglUsers.size()>=GglMax){
                return;
            }else {
                for(int i = 0; i < GglMin; i++){
                    GglUser  gglUser = new GglUser();
                    gglUser.setStatus(0);
                    gglUser.setType(1);
                    gglUser.setHdType(0);
                    gglUser.setGainDate(gainDate);
                    gglUser.setMemberId(member.getId());
                    gglUser.setMobile(member.getMobile());
                    save(gglUser);
                }

            }
        }


    }

}
